package main.Q2101_2200;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Q2191_2200 {
    public static void main(String[] args) {
        System.out.println("Question2191：");
        System.out.println("Question2192：");
        System.out.println("Question2193：");
        System.out.println("Question2194：");
        System.out.println("Question2195：");
        System.out.println("Question2196：根据描述创建二叉树");
        System.out.println("Question2197：");
        System.out.println("Question2198：");
        System.out.println("Question2199：");
        System.out.println("Question2200：");
    }
}

class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;

    TreeNode() {
    }

    TreeNode(int val) {
        this.val = val;
    }

    TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}

class Question2196 {
    public TreeNode createBinaryTree(int[][] descriptions) {
        Map<Integer, TreeNode> nodes = new HashMap<>();
        for (int[] node : descriptions) {
            TreeNode curRoot = new TreeNode(node[0]);
            nodes.put(node[0], curRoot);
            TreeNode curSon = new TreeNode(node[1]);
            nodes.put(node[1], curSon);
        }
        int root = -1;
        List<Integer> exitParent = new ArrayList<>();
        for (int[] node : descriptions) {
            TreeNode parent = nodes.get(node[0]);
            TreeNode son = nodes.get(node[1]);
            exitParent.add(node[1]);
            if (node[2] == 1) {
                parent.left = son;
            } else {
                parent.right = son;
            }
        }
        for (int num : nodes.keySet()) {
            if (!exitParent.contains(num)) {
                root = num;
                break;
            }
        }
        return nodes.get(root);
    }
}